//
//  HomeInviteVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/3/24.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "HomeInviteVC.h"
#import "HomeInviteModel.h"
#import "HomeInviteCell.h"

@interface HomeInviteVC () <UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <HomeInviteModel *>*dataArray;

@end

@implementation HomeInviteVC

-(NSMutableArray<HomeInviteModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UIView *)searchView {
    if (!_searchView) {
        _searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 54)];
        _searchView.backgroundColor = HexColor(0xffffff);
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.searchView.nim_width - 20, 34)];
        bgView.backgroundColor = HexColor(0xF2F2F5);
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 4;
        [_searchView addSubview:bgView];
        
        UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(bgView.nim_width - 10 - 30, 7, 30, 20)];
        [searchBtn setTitle:@"搜索" forState:0];
        [searchBtn setTitleColor:Color_ThemeColor forState:0];
        searchBtn.titleLabel.font = BoldFontOfSize(14);
        [searchBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:searchBtn];
        
        self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(10, 7, searchBtn.nim_left - 10 - 10, 20)];
        self.searchField.placeholder = @"货主名称";
        self.searchField.font = [UIFont systemFontOfSize:14];
        [bgView addSubview:self.searchField];
    }
    return _searchView;
}

- (void)searchClick {
    [self requestForList];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchView.nim_bottom, ScreenW, ScreenH - BottomHeight - NaviHeight - 54) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isDriverInvite ? @"云车主邀请" : @"3PL邀请";
    page = 1;
    [self.view addSubview:self.searchView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:self.isDriverInvite ?  LogisticsSupplierDanCheInviteList_V_1_0 : LogisticsSupplierDuoCheInviteList_V_1_0 params:@{@"page":@(page),@"keyword":self.searchField.text.length ? self.searchField.text : @""} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [HomeInviteModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [HomeInviteModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)requesrForSure:(NSString *)Id {
    [HttpRequest PostWithApi:self.isDriverInvite ? LogisticsSupplierDanCheAcceptInvite_V_1_0 : LogisticsSupplierDuoCheAcceptInvite_V_1_0 params:@{@"id":Id} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self->page = 1;
            [self requestForList];
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已确认"];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeInviteCell *cell = [HomeInviteCell HomeInviteCellWithTableView:tableView];
    HomeInviteModel *model = self.dataArray[indexPath.section];
    cell.model = model;
    __weak_self;
    cell.sureBlock = ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定接受邀请？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakself requesrForSure:model.Id];
        }];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:sureAction];
        [alert addAction:cancleAction];
        [weakself presentViewController:alert animated:YES completion:nil];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeInviteCell *cell = [HomeInviteCell HomeInviteCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
        view.backgroundColor = Color_backgroundColor;
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
